// pli.cpp : Defines the entry point for the DLL application.
//


#include <windows.h> 
#include <stdio.h>
#include <conio.h>
#include <tchar.h>

#include "pli.h"
#include "msg.h"


#ifdef _MANAGED
#pragma managed(push, off)
#endif
int init();
void cleanup();

//#define DEBUG



#if 1

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		if(init()==-1) return FALSE;
	break;
	case DLL_THREAD_ATTACH:

	
		break;
	case DLL_THREAD_DETACH:

		break;
	case DLL_PROCESS_DETACH:

		cleanup();
		break;
	}
    return TRUE;
}
#else

int _tmain(int argc, char *argv[]) 
{ 
	
	init();
	PIPE_MSG  msg;
	int err;
	char buffer[BUFSIZE];
	msg.hdr.size=SIZE_MREAD;
	msg.u.mread.address_hi=0;
	msg.u.mread.address_lo=0xfffffff0;
	msg.hdr.count=0x10;
	msg.hdr.type=MREAD;
	if((err=rpc_call(&msg,buffer))<0)
	{
		printf("rpc_call failed.%d\n",err);
	}
}

#endif
HANDLE hPipe; 
//LPTSTR lpvMessage=TEXT("Default message from client."); 
//TCHAR  chBuf[BUFSIZE]; 

//DWORD  cbRead, cbToWrite, cbWritten, ; 
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\mynamedpipe"); 


 
int init()
{ 
	DWORD dwMode;
	BOOL   fSuccess = FALSE; 

 
// Try to open a named pipe; wait for it, if necessary. 
 
   while (1) 
   { 
      hPipe = CreateFile( 
         lpszPipename,   // pipe name 
         GENERIC_READ |  // read and write access 
         GENERIC_WRITE, 
         0,              // no sharing 
         NULL,           // default security attributes
         OPEN_EXISTING,  // opens existing pipe 
         0,              // default attributes 
         NULL);          // no template file 
 
   // Break if the pipe handle is valid. 
 
      if (hPipe != INVALID_HANDLE_VALUE) 
         break; 
 
      // Exit if an error other than ERROR_PIPE_BUSY occurs. 
 
      if (GetLastError() != ERROR_PIPE_BUSY) 
      {
         io_printf( ("Could not open pipe. GLE=%d\n"), GetLastError() ); 
         return -1;
      }
 
      // All pipe instances are busy, so wait for 20 seconds. 
 
      if ( ! WaitNamedPipe(lpszPipename, 20000)) 
      { 
         io_printf("Could not open pipe: 20 second wait timed out."); 
         return -1;
      } 
   } 
 
// The pipe connected; change to message-read mode. 
 
   dwMode = PIPE_READMODE_MESSAGE; 
   fSuccess = SetNamedPipeHandleState( 
      hPipe,    // pipe handle 
      &dwMode,  // new pipe mode 
      NULL,     // don't set maximum bytes 
      NULL);    // don't set maximum time 
   if ( ! fSuccess) 
   {
      io_printf( ("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError() ); 
      return -1;
   }

   return 0;
}

bool on;

int rpc_call(PIPE_MSG *msg,char * reply)
{
 	BOOL   fSuccess = FALSE; 
	DWORD  cbRead,cbWrite;
// Send a message to the pipe server. 
 
#ifdef DEBUG
   io_printf( ("\nSending %d byte message: %x\n"), msg->hdr.size,msg->hdr.type); 
   for(int i=0;i<msg->hdr.size;i++) io_printf("%02x ",((unsigned char *)msg)[i]);
   io_printf("\n");
#endif

   fSuccess = WriteFile( 
      hPipe,                  // pipe handle 
      msg,             // message 
      msg->hdr.size,              // message length 
      &cbWrite,             // bytes written 
      NULL);                  // not overlapped 

   if ( ! fSuccess) 
   {
      io_printf( ("WriteFile to pipe failed. GLE=%d\n"), GetLastError() ); 

      return -1;
   }

   FlushFileBuffers(hPipe); 

#ifdef DEBUG
   io_printf("\nMessage sent to server.\n");
#endif

   do 
   { 
   // Read from the pipe. 
 
      fSuccess = ReadFile( 
         hPipe,    // pipe handle 
         reply,    // buffer to receive reply 
         BUFSIZE,  // size of buffer 
         &cbRead,  // number of bytes read 
         NULL);    // not overlapped 
 
      if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
         break; 

   } while ( ! fSuccess);  // repeat loop if ERROR_MORE_DATA 

   if ( ! fSuccess)
   {
      io_printf( ("ReadFile from pipe failed. GLE=%d\n"), GetLastError() );
      return -1;
   }
#ifdef  DEBUG
   PIPE_MSG *rmsg=(PIPE_MSG *)reply;
   io_printf("\nreply from server.(%x)\n",rmsg->hdr.count);

   for(int i=0;i<rmsg->hdr.count;i++) 
   {

	   io_printf("%02x ",(unsigned char )reply[SIZE_REPLY+i]);
	   if((i&0xf)==0xf) io_printf("\n");
   }
   io_printf("\n");
#endif

   void assert_intr(bool on);

   assert_intr(on);
   on=!on;



 
   return 0; 
}

void cleanup()
{
   io_printf("\nEnd of message,to terminate connection and exit...");
  // _getch();
 
   CloseHandle(hPipe); 
 
}



